package listbyorder.access101_200.test150;

import java.util.Stack;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/12 12:40
 */
public class Solution1 {

    // 求解逆波兰表达式
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].equals("+")) {
                stack.push(stack.pop() + stack.pop());
            } else if (tokens[i].equals("-")) {
                int n1 = stack.pop();
                int n2 = stack.pop();
                stack.push(n2 - n1);
            } else if (tokens[i].equals("/")) {
                int n1 = stack.pop();
                int n2 = stack.pop();
                stack.push(n2 / n1);
            } else if (tokens[i].equals("*")) {
                int n1 = stack.pop();
                int n2 = stack.pop();
                stack.push(n2 * n1);
            } else {
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        return stack.pop();
    }

}
